package com.learning.spring.cloud.alibaba.feign.user.config;

import com.learning.spring.cloud.alibaba.ribbon.user.web.service.OrderFeignService;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.Util;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * ClassName: FeignConfig
 * Description: Feign 配置文件
 * <p>
 * 1. 手动创建 Feign，不需要添加注解 @EnableFeignClients
 * 2. 依赖 FeignClientsConfiguration
 * Date: 2019/6/4 16:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Import(FeignClientsConfiguration.class)
@Configuration
public class FeignConfig {
    @Bean
    public OrderFeignService userUserFeignService(Decoder decoder, Encoder feignEncoder, Client feignClient, Contract contract) {
        // 这边的decoder、encoder、client、contract，可以debug看看是什么实例。
        return Feign.builder()
                .client(feignClient)
                .encoder(feignEncoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "user", Util.UTF_8))
                .target(OrderFeignService.class, "http://01-ms-feign-order/");
    }


    @Bean
    public OrderFeignService adminUserFeignService(Decoder decoder, Encoder feignEncoder, Client feignClient, Contract contract) {
        // 这边的 decoder、encoder、client、contract，可以 debug 看看是什么实例。
        return Feign.builder()
                .client(feignClient)
                .encoder(feignEncoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin", Util.UTF_8))
                .target(OrderFeignService.class, "http://01-ms-feign-order/");
    }

}
